package edu.biwu.response;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

/**
 * HttpServletResponse操作字符流写中文
 *      public String getCharacterEncoding():获取字符输出流中默认使用的码表
 * 浏览器乱码的产生:
 *     1.response.getWriter().write("你好") 进行查询UTF-8码表(1个中文3个字节)=>E4 BD A0 E5 A5 BD
 *     2.浏览器默认使用系统码表(GBK)=>浏览器会将E4 BD A0 E5 A5 BD查询GBK码表(1个中文2个字节)=>浣犲ソ
 * 解决方法：
 *       response.setContentType("text/html;charset=UTF-8")
 *    原理:
 *     1.会自动设置字符流缓冲区的码表,设置为UTF-8 相当于 response.setCharacterEncoding("UTF-8");
 *     2.会设置一个响应头 Content-Type: text/html;charset=UTF-8,浏览器接收这个响应头之后就知道要将响应的数据以UTF-8进行解析
 *
 */
@WebServlet("/resp/demo04")
public class ResponseDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset = UTF-8");
        response.getWriter().write("你好");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
